# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.


library(tidyverse)
library(lubridate)
library(RColorBrewer)
library(patchwork)

# This file looks at the types of Acronym spending.

df_acronym <- read_csv("ad_library_2020_acronym.csv",
                       col_types = c(page_id = "c"))

acronym_domains <-
  c(
    "www.showuptovote.com",
    "action.fourisenough.org",
    "www.trumpcoronavirusplan.com",
    "trumpcoronavirusplan.com",
    "cheatsheetforthevotingbooth.com",
    "action.prjctsunshine.org",
    "",
    NA
  )

turnout_phrases <-
  "(make a plan|ilana|vote biden to get it done|vote to ditch trump|use your power and vote|running out to vote|make your plan|cheat sheet for the voting)"




df_acronym <-
  df_acronym %>%
  mutate(
    external_domain = str_trim(str_remove_all(ad_creative_link_captions, ",")),
    external_domain = ifelse(external_domain %in% acronym_domains, NA, external_domain)
  ) %>%
  mutate(
    ad_type = case_when(is_trump & is_biden ~ "Both",
                        is_trump ~ "Trump",
                        is_biden ~ "Biden"),
    month = as.Date(floor_date(ad_delivery_start_time, unit = "month")),
    spend_ub = ifelse(spend_ub == -1, 1000000, spend_ub),
    ad_format = case_when(
      is_video_acronym ~ "Traditional\nvideo",
      !is.na(external_domain) ~ "Promoted\nnews",
      TRUE ~ "Other"
    ),
    is_turnout = (
      ad_creative_link_captions %in% c("www.showuptovote.com", "cheatsheetforthevotingbooth.com")
    )
    | str_detect(tolower(ad_creative_bodies), turnout_phrases)
  )

gg_df_1 <-
  df_acronym %>%
  filter(!is_turnout) %>%
  rename(`Ad Format` = ad_format) %>%
  group_by(`Ad Format`, month) %>%
  summarize(spend_lb = sum(spend_lb))
  
g1 <-
  ggplot(gg_df_1, aes(x = month, y = spend_lb, color = `Ad Format`,
                      shape = `Ad Format`)) +
  geom_point() +
  geom_line() +
  ylab("ACRONYM Facebook Spending, Lower Bound") +
  xlab("Month") +
  scale_color_brewer(palette = "Set2") +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        legend.position = "bottom",
        legend.text = element_text(size = 7),
        legend.title = element_text(size = 8)) +
  scale_y_continuous(labels = scales::dollar_format(),limits = c(0, 6e5)) +
  scale_x_date(labels=scales::date_format(format = "%B %Y")) 

gg_df_2 <-
  df_acronym %>%
  filter(!is_turnout) %>%
  rename(`Keyword` = ad_type) %>%
  group_by(Keyword, month) %>%
  summarize(spend_lb = sum(spend_lb))

g2 <- 
    ggplot(gg_df_2, aes(x = month, y = spend_lb, group = Keyword, color = Keyword, 
                        shape = Keyword)) +
    geom_line() +
    geom_point() +
    ylab("ACRONYM Facebook Spending, Lower Bound") +
    xlab("Month") +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        legend.position = "bottom",
        legend.text = element_text(size = 7),
        legend.title = element_text(size = 8)) +
    scale_color_brewer(palette = "Set1") + 
  scale_y_continuous(labels = scales::dollar_format(),limits = c(0, 6e5)) +
  scale_x_date(labels = scales::date_format(format = "%B %Y")) 


g <- g1 + g2

# ggsave(
#   "output/figure_B2.pdf",
#   g,
#   height = 4,
#   width = 8
# )